var userActions = require('../../util/user-actions.js');

//引入代码
var call = require('../../util/request.js');

Page({
	data: {
		background: ["demo-text-1", "demo-text-2", "demo-text-3"],
		indicatorDots: true,
		vertical: false,
		autoplay: true,
		interval: 5000,
		duration: 500,
		pictureList: [],
		
	},
	onShareAppMessage: function (opt) {
	  this.setData({
	    from: opt.from
	  });
	  return Object.assign({}, this.data.shareData, {
	    title: opt.from === 'button' ? '这是要分享的小程序标题' : '宁波海洋世界门票购买',
	    path: '/pages/API/share/share?a=b&from=' + opt.from,
	    imageUrl: 'http://nbhysj.com/web/new_index/images/logo.png',
	    success (res) {},
	    fail (err) {}
	  })
	},
	//跳转地图
	getLocation() {
		tt.getLocation({
			type: 'gcj02',
			success(res) {
				tt.openLocation({
					longitude: Number(121.583035),
					latitude: Number(29.853165),
					name: '宁波神凤海洋世界',
					address: '宁波市鄞州区桑田路936号',
					scale: 18,
					success() {
						tt.showToast({
							title: '成功',
						});
					},
					fail(res) {
						tt.showToast({
							title: res.errMsg,
						});
					}
				});
			},
			fail(res) {
				tt.showToast({
					title: res.errMsg,
				})
			}
		});
	},
	onShow() {
		var that = this;
		userActions.getUserInfo().then(loginInfo => {
			//调用封装的方法，为了方便我直接在页面加载的时候执行这个方法
			call.getData('api/code2Session?code='+loginInfo.code, this.shuffleSuc, this.fail);
		});
		
		//轮播图
		call.getData('api/mchPhoneList', this.supplier, this.fail);
		
		//商户信息
		call.getData('api/getMchInfo', this.message, this.fail);
		
		//门票
		call.getData('api/getTicket', this.tick, this.fail);
	},
	
	tick:function(data){
		var that = this;
		that.setData({
			tick: data.data
		})
	},
	message:function(data){
		var that = this;
		that.setData({
			mess: data.data
		})
	},
	
	//图片
	supplier:function(data){
		var that = this;
		that.setData({
			photos: data.data
		})
	},
	shuffleSuc: function(data) {
		var that = this;
		tt.setStorageSync("key", data.data.openid);
		
		tt.getUserInfo({
			withCredentials: that.data.withCredentials,
			success: function (res) {
				var postData={
					nickName:res.userInfo.nickName,
					avatarUrl:res.userInfo.avatarUrl,
					gender:res.userInfo.gender,
					city:res.userInfo.city,
					province:res.userInfo.province,
					country:res.userInfo.country,
					language:res.userInfo.language,
					openId:tt.getStorageSync("key")
				}
				call.request('api/setUserInfo',postData,that.setUserInfo, that.fails);
			},
			// fail () {
			// 	tt.showModal({
			// 		title: '调用 getUserInfo 失败，检查是否需要 login'
			// 	})
			// }
		});
		
		
		// that.setData({
		// 	pictureList: data.rows
		// })
		
	},
	
	fail: function() {
		// console.log('用户信息2' +that.loginInfo)
		// console.log("失败")
	},

	//跳转景区信息
	navigateTo: function() {
		console.log('事件')
		tt.navigateTo({
			url: '/pages/attractions/attractions'
		})
	},
	//购票须知
	infor: function(e) {
		tt.navigateTo({
			url: '/pages/information/information?name='+JSON.stringify(e.currentTarget.dataset.name)
		})
	},

	getorder: function(item) {
		tt.navigateTo({
			url: '/pages/order/order?id='+item.currentTarget.dataset.index + '&name='+JSON.stringify(item.currentTarget.dataset.title),
		})
	},
	changeIndicatorDots: function(e) {
		this.setData({
			indicatorDots: !this.data.indicatorDots
		});
	},
	changeAutoplay: function(e) {
		this.setData({
			autoplay: !this.data.autoplay
		});
	},
	intervalChange: function(e) {
		this.setData({
			interval: e.detail.value
		});
	},
	durationChange: function(e) {
		this.setData({
			duration: e.detail.value
		});
	}
});
